﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using UBCon.Ribbon.Interfaces;

namespace UBCon.Ribbon
{
    internal class RibbonGroupPanel : Panel, IRibbonControl
    {
        #region Overrieds
        protected override Size ArrangeOverride(Size finalSize)
        {
            double max_y = 0, max_x = 0, y = 0, x = 0;
            Size size = new Size();
            UIElement e = null;
            Rect rect = new Rect();
                foreach (int index in this.largeItems)
                {
                    e = InternalChildren[index];
                    rect.X = x;
                    rect.Y = 0;
                    rect.Width = e.DesiredSize.Width;
                    rect.Height = e.DesiredSize.Height;
                    e.Arrange(rect);
                    x += e.DesiredSize.Width;
                    size.Width += e.DesiredSize.Width;
                    size.Height = Math.Max(size.Height, e.DesiredSize.Height);
                }

                    for (int i = 0; i < smalItems.Count; i++)
                    {
                        if (i != 0 && i % 3 == 0)
                        {
                            size.Width += max_x;
                            x = size.Width;
                            max_y = Math.Max(max_y, y);
                            max_x = 0;
                            y = 0;
                        }
                        e = InternalChildren[this.smalItems[i]];
                        rect.X = x;
                        rect.Y = y;
                        rect.Width = e.DesiredSize.Width;
                        rect.Height = e.DesiredSize.Height;
                        e.Arrange(rect);
                        y += e.DesiredSize.Height;
                        max_x = System.Math.Max(max_x, e.DesiredSize.Width);
                    }
                    size.Width += max_x;
                    max_y = Math.Max(max_y, y);
                    size.Height = Math.Max(size.Height, max_y);
            return size;

        }

        protected override Size MeasureOverride(Size availableSize)
        {
            Size infiniteSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
            if (smalItems == null && largeItems == null)
            {
                largeItems = new List<int>();
                smalItems = new List<int>();
                for (int i = 0; i < InternalChildren.Count; i++)
                {
                    IRibbonControl btn = InternalChildren[i] as IRibbonControl;
                    if (btn != null)
                    {
                        if (btn.IsLarge)
                        {
                            if (btn is RibbonGallery) 
                            {
                                RibbonGallery gallery = btn as RibbonGallery;
                                gallery.SetValue(RibbonGallery.IsInGroupProperty, true);
                            }
                            largeItems.Add(i);
                        }
                        else
                        {
                            smalItems.Add(i);
                        }
                    }

                }
            }
            foreach (UIElement elt in InternalChildren)
            {
                elt.Measure(infiniteSize);
            }
           Size size = CalculateExtent();
            if (!double.IsInfinity(availableSize.Height))
            {
                size.Height = Math.Max(size.Height, availableSize.Height);
            }

            return size;
        }
        #endregion

        #region Private Utilities
        private System.Windows.Size CalculateExtent()
        {
            double max_y = 0, max_x = 0, y = 0;
            System.Windows.UIElement e = null;
            System.Windows.Size size = new System.Windows.Size();
            
                foreach (int index in this.largeItems)
                {
                    e = this.InternalChildren[index];
                    size.Width += e.DesiredSize.Width;
                    size.Height = System.Math.Max(size.Height, e.DesiredSize.Height);
                }
                
                  for (int i = 0; i < this.smalItems.Count; i++)
                    {
                        if (i != 0 && i % 3 == 0)
                        {
                            size.Width += max_x;
                            max_y = System.Math.Max(max_y, y);
                            max_x = 0;
                            y = 0;
                        }
                        e = this.InternalChildren[this.smalItems[i]];
                        y += e.DesiredSize.Height;
                        max_x = System.Math.Max(max_x, e.DesiredSize.Width);
                    }
                    size.Width += max_x;
                    max_y = System.Math.Max(max_y, y);
                    size.Height = System.Math.Max(size.Height, max_y);
            
            return size;
        }
        #endregion

        #region Fields
        private System.Collections.Generic.List<int> smalItems = null;
        private System.Collections.Generic.List<int> largeItems = null;
        #endregion

        #region IRibbonControl Implementation
        public Interfaces.IRibbonControl Clone()
        {
            return null;
        }

        public bool IsLarge
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsClonedItem
        {
            get { return false; }
        }

        public bool IsCloneable
        {
            get { return false; }
        }


        public void DisposeControl()
        {
            
        }

        public Interfaces.IRibbonControl ParentControl
        {
            get { return this.VisualParent as IRibbonControl; }
        }
        #endregion
    }
}
